package zn.io.fastframe.modules.sys.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;
import zn.io.fastframe.common.utils.R;
import zn.io.fastframe.modules.sys.entity.SysUserEntity;
import zn.io.fastframe.modules.sys.form.SysLoginForm;
import zn.io.fastframe.modules.sys.service.SysCaptchaService;
import zn.io.fastframe.modules.sys.service.SysUserService;
import zn.io.fastframe.modules.sys.service.SysUserTokenService;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录相关
 */

@RestController
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysCaptchaService sysCaptchaService;

    @Autowired
    private SysUserTokenService sysUserTokenService;

    /**
     * 获取验证图片
     *
     * @param response
     * @param uuid
     */
    @GetMapping("captcha.jpg")
    public Map<String, Object> captcha(HttpServletResponse response, String uuid) throws ServletException, IOException {
        System.out.println(uuid);
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");


        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        BufferedImage bufferedImage = sysCaptchaService.getCaptcha(uuid);
//        System.out.println(bufferedImage);
//        ServletOutputStream outputStream = response.getOutputStream();
//        ImageIO.write(bufferedImage, "jpg", outputStream);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", outputStream);

        BASE64Encoder encoder = new BASE64Encoder();
        String base64Img = encoder.encode(outputStream.toByteArray());
        outputStream.close();
        R captcha = R.ok().put("captcha", "data:image/png;base64," + base64Img);

        System.out.println(captcha);
        return captcha;
    }

    @PostMapping("/sys/login")
    public Map<String, Object> login(@RequestBody SysLoginForm sysLoginForm) {
        // 先不做验证码验证
        boolean validate = sysCaptchaService.validate(sysLoginForm.getUuid(), sysLoginForm.getCaptcha());
        if (validate) {
            return R.error("验证码不正确");
        }

        //获取用户信息
        SysUserEntity sysUserEntity = sysUserService.queryByUserName(sysLoginForm.getUsername());
        if (sysUserEntity == null) {
            return R.error("用户不存在");
        }
        if (!sysUserEntity.getPassword().equals(sysLoginForm.getPassword())) {
            return R.error("密码不正确");
        }

        if(sysUserEntity.getStatus() == 0) {
            return R.error("账户已被锁定，请练下管理员");
        }

        R r = sysUserTokenService.createToken(sysUserEntity.getUserId());
        return r;
    }

}
